<template>
  <div class="cate-container">
    <!-- 面包屑导航 -->
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>-->
    <bread :second="'商品管理'" :third="'商品分类'"></bread>
    <!-- 卡片 -->
    <el-card class="box-card">
      <div style="margin-top: 15px;">
        <el-button type="primary" @click="cateAdd">添加分类</el-button>
        <!-- 表格 -->
        <!-- <el-table :data="cateList" border="" style="margin: 15px 0;width: 100%">
          <el-table-column type="index" width="50" label="#"></el-table-column>
          <el-table-column prop="cat_name" label="分类名称" width="180"></el-table-column>
          <el-table-column prop="cat_deleted" label="是否有效" width="180">
            <template slot-scope="scoped">
              <i v-if="scoped.row.cat_deleted" class="el-icon-error"></i>
              <i style="color:green" v-else class="el-icon-success"></i>
            </template>
          </el-table-column>
          <el-table-column label="排序">
            <template slot-scope="scoped">
              <el-tag v-if="scoped.row.cat_level==0">一级</el-tag>
              <el-tag v-else-if="scoped.row.cat_level==1" type="success">二级</el-tag>
              <el-tag v-else type="warning">三级</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作"></el-table-column>
        </el-table>======-->

        <zk-table
          :data="cateList"
          :columns="columns"
          :selection-type="false"
          :expand-type="false"
          :show-index="true"
        >
          <template slot="cat_deleted" slot-scope="scoped">
            <!--  ... ... (图中对应状态栏的绿点儿)-->
            <!-- {{scope.row.cat_deleted}}  -->
            <i v-if="scoped.row.cat_deleted" class="el-icon-error"></i>
            <i style="color:green" v-else class="el-icon-success"></i>
          </template>
          <template slot="cat_level" slot-scope="scoped">
            <!--  ... ... (图中对应状态栏的绿点儿)-->
            <!-- {{scope.row.cat_level}} -->
            <el-tag v-if="scoped.row.cat_level==0">一级</el-tag>
            <el-tag v-else-if="scoped.row.cat_level==1" type="success">二级</el-tag>
            <el-tag v-else type="warning">三级</el-tag>
          </template>
          <template slot="cat_id" slot-scope="scoped">
            <!--  ... ... (图中对应状态栏的绿点儿)-->
            {{scoped.row.cat_id}}
          </template>
        </zk-table>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="cateParams.pagenum"
          :page-sizes="[1, 2, 3, 4]"
          :page-size="cateParams.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
        ={{total}}==
        <!-- 添加分类的表单对话框 -->
        <el-dialog title="添加分类" :visible.sync="dialogFormVisibleAdd">
          <el-form ref="formDialogAddCate" :model="formAddCate" :rules="rulesFormAddCate">
            <el-form-item prop="cat_name" label="分类名称" label-width="120px">
              <el-input v-model="formAddCate.cat_name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="父级分类" label-width="120px">
              <!-- //级联选择器 -->
              <el-cascader
                v-model="value"
                :options="cateCasList"
                :props="props"
                @change="handleChange"
                clearable
                change-on-select
              ></el-cascader>
              ===={{value}}===
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addForm">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>
           
<script>
import { cateList_api, cateAdd_api } from '@/api'
export default {
  data() {
    return {
      cateParams: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      cateList: [],
      total: 1,
      //表格标题数据
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'cat_deleted'
        },
        {
          label: '排序',
          prop: 'cat_level',
          type: 'template',
          template: 'cat_level'
        },
        {
          label: '操作',
          prop: 'cat_id',
          type: 'template',
          template: 'cat_id'
        }
      ],

      /* 添加分类 */
      dialogFormVisibleAdd: false,
      formAddCate: {
        cat_pid: '',
        cat_name: '',
        cat_level: ''
      },
      cateCasList: [],
      //  表单校验规则
      rulesFormAddCate: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
          //   { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },

      /* 级联续择器 */
      value: [],
      props: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      }
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    //   添加分类
    async cateAdd() {
      this.dialogFormVisibleAdd = true
      //获取父级分类
      const { data: res } = await cateList_api({ type: 2 })
      console.log(res, 5555)
      this.cateCasList = res.data
    },
    //级联选择器的事件
    handleChange() {},
    //点击对话框的确认按钮
    addForm() {
      this.$refs.formDialogAddCate.validate(async valide => {
        if (!valide) {
          return
        } else {
          //  var  arr=[1,2]   索引：arr.length-1 =1     根据索引获取值：arr[arr.length-1]=2
          //获取数组中最后一个的值
          if (this.value.length > 0) {
            this.formAddCate.cat_pid = this.value[this.value.length - 1]
          } else {
            this.formAddCate.cat_pid = 0
          }

          this.formAddCate.cat_level = this.value.length
          console.log(this.value[this.value.length - 1], 7788)

          //  发送请求
          const { data: res } = await cateAdd_api(this.formAddCate)
          console.log(res, 3333)
          this.dialogFormVisibleAdd = false
          this.getCateList()
          this.$refs.formDialogAddCate.resetFields()
          this.value = []
          //  清空表单
          this.formAddCate = {
            cat_pid: '',
            cat_name: '',
            cat_level: ''
          }
        }
      })
    },
    async getCateList() {
      const { data: res } = await cateList_api(this.cateParams)
      console.log(res, 66666)
      this.cateList = res.data.result
      this.total = res.data.total
    },
    /* 分页 */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.cateParams.pagesize = val
      this.getCateList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.cateParams.pagenum = val
      this.getCateList()
    }
  }
}
</script>
<style scoped lang='less'>
.cate-container {
  .el-breadcrumb {
    margin-bottom: 20px;
  }
}
</style>